package com.example.finalexamcovid_19situations;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.view.View;

public class MaDaWeiDraw extends View{

    //原点
    public int XPoint=40+40;   //原点的X坐标，原为40，加40扩大左边距离
    public int YPoint=Constant.point.y/2;   //原点的Y坐标

    //刻度长度
    public int XScale=55;   //X的刻度长度
    public int YScale=40;   //Y的刻度长度

    //轴长度
    public int XLength=Constant.point.x-100;    //X轴的长度
    public int YLength=Constant.point.y/2-100;  //Y轴的长度

    //轴的值
    public String[] XLabel; //X的刻度
    public String[] YLabel; //Y的刻度

    public String[] Data;   //数据
    public String Title;    //显示的标题

    //默认构造函数
    public MaDaWeiDraw(Context context)
    {
        super(context);
    }

    //设置值
    public void SetInfo(String[] XLabels,String[] YLabels,String[] AllData,String strTitle)
    {
        XLabel=XLabels;
        YLabel=YLabels;
        Data=AllData;
        //判断传入，为负则自动减最小刻度，变正数
        if (Integer.parseInt(YLabel[0])<0){
            for (int i = 0; i< Data.length; i++){
                Data[i]=String.valueOf(Integer.parseInt(Data[i])-Integer.parseInt(YLabel[0]));
            }
        }

        Title=strTitle;
        XScale=XLength/AllData.length;
        YScale=YLength/YLabels.length;
    }

    public static class Constant {
        public static Point point;  //获取屏幕的大小
    }

    //具体绘画方法
    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);   //重写onDraw方法

        //创建画笔
        Paint paint= new Paint();
        paint.setStyle(Paint.Style.STROKE); //设置背景颜色
        paint.setAntiAlias(true);   //去锯齿
//        paint.setColor(Color.BLACK);    //设置颜色

        paint.setColor(Color.rgb(90,120,65));    //设置颜色
        paint.setTextSize(20);  //设置轴文字大小

//设置Y轴
        //y轴轴线
        canvas.drawLine(XPoint, YPoint-YLength, XPoint, YPoint, paint);

        //使用for循环绘制刻度和坐标值
        for(int i=0;i*YScale<YLength ;i++)
        {
            //绘制刻度
            canvas.drawLine(XPoint,YPoint-i*YScale, XPoint+5, YPoint-i*YScale, paint);

            //绘制文字
            try
            {
                //y轴文字，将文字向左36后，再向左40，文字向下30后向上20
                canvas.drawText(YLabel[i] , XPoint-36-40, YPoint-i*YScale+30-20, paint);
            }
            catch(Exception e)
            {
            }
        }

        //y轴箭头左线和箭头右线
        canvas.drawLine(XPoint,YPoint-YLength,XPoint-3,YPoint-YLength+6,paint);
        canvas.drawLine(XPoint,YPoint-YLength,XPoint+3,YPoint-YLength+6,paint);

        //写坐标说明
        drawText(canvas,"单位:人", XPoint-5, YPoint-YLength+YScale-5, paint,-90);  //设置角度等

//设置X轴
        //x轴轴线
        canvas.drawLine(XPoint,YPoint,XPoint+XLength,YPoint,paint);

        //使用for循环绘制刻度和坐标值
        for(int i=0;i*XScale<XLength;i++)
        {
            //画x轴刻度
            canvas.drawLine(XPoint+i*XScale, YPoint, XPoint+i*XScale, YPoint-5, paint);

            //绘制文字和折线
            try
            {
                //画x轴文字
                drawText(canvas,XLabel[i], XPoint + i * XScale, YPoint + 30, paint,0);  // 文字

                //根据数据绘制折线，如果i>0且左和现在数据均为有效数据则进行绘制
                if (i > 0 && YCord(Data[i - 1]) != Integer.MAX_VALUE && YCord(Data[i]) != Integer.MAX_VALUE)  // 保证有效数据
                    canvas.drawLine(XPoint + (i - 1) * XScale, YCord(Data[i - 1]),
                            XPoint + i * XScale, YCord(Data[i]), paint);

                //画数据点
                canvas.drawCircle(XPoint + i * XScale, YCord(Data[i]), 2, paint);

                //调试代码，数据显示在在坐标轴
//                drawText(canvas,String.valueOf(YCord(Data[i])), XPoint + i * XScale, YPoint + 30+30, paint,0);
//                drawText(canvas,String.valueOf(Data[i]), XPoint + i * XScale, YPoint + 30+30+30, paint,0);
//                drawText(canvas,String.valueOf(YCord(Data[i])), XPoint + i * XScale, YCord(Data[i])-30, paint,0);
                //显示数据，判断为负则加最小刻度
                if (Integer.parseInt(YLabel[0])>0){
                    drawText(canvas,Data[i], XPoint + i * XScale, YCord(Data[i])-30, paint,0);
                }else {
                    drawText(canvas,String.valueOf(Integer.parseInt(Data[i])+
                            Integer.parseInt(YLabel[0])), XPoint + i * XScale,
                            YCord(Data[i])-30, paint,0);
                    //调试代码，数据显示在在坐标轴
//                    drawText(canvas,Data[i], XPoint + i * XScale, YCord(Data[i])-60, paint,0);
                }

            } catch (Exception e) {
            }
        }

        //箭头上线和箭头下线
        canvas.drawLine(XPoint+XLength,YPoint,XPoint+XLength-6,YPoint-3,paint);
        canvas.drawLine(XPoint+XLength,YPoint,XPoint+XLength-6,YPoint+3,paint);

        //设置标题大小和位置
        paint.setTextSize(30);
        canvas.drawText(Title, XLength/2-28, 50, paint);
    }

    //画文字的函数，画x轴文字和y轴文字时调用
    void drawText(Canvas canvas ,String text , float x ,float y,Paint paint ,float angle){
        if(angle != 0){
            canvas.rotate(angle, x, y);
        }
        canvas.drawText(text, x, y, paint);
        if(angle != 0){
            canvas.rotate(-angle, x, y);
        }
    }

    //计算y轴坐标，绘制折线时调用
    private int YCord(String y0)
    {
        //将sting数值转为int数值，错误返回Integer.MAX_VALUE
        int y;
        try
        {
            y=Integer.parseInt(y0);
        }
        catch(Exception e)
        {
            return Integer.MAX_VALUE;    //出错则返回Integer.MAX_VALUE
        }
        //返回y的坐标
        try
        {
            //获取最大值
            double max=Double.parseDouble(YLabel[YLabel.length-1]);
            double min=Double.parseDouble(YLabel[0]);
            //判断最小值正负
            if (min<0){
                max=Math.abs(min-max);
            }
            //计算y坐标值，y和max的比例乘Y轴刻度长度乘y轴刻度数量
            double calculate=y/max*YScale*(YLabel.length-1);
            //返回原点y坐标减计算出的y坐标
            return YPoint-(int)Math.round(calculate);
        }
        catch(Exception e)
        {
        }
        return y;
    }
}
